package com.jzlw.huozhuduan.base;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;


import com.blankj.utilcode.util.ToastUtils;
import com.jzlw.huozhuduan.R;

import com.jzlw.huozhuduan.dialog.MainDialog;
import com.jzlw.huozhuduan.dialog.MyPhotoDialog;
import com.jzlw.huozhuduan.dialog.ShowDialog;
import com.jzlw.huozhuduan.skeleton.Skeleton;
import com.jzlw.huozhuduan.skeleton.ViewSkeletonScreen;
import com.jaeger.library.StatusBarUtil;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * activity 的基类。
 **/
public abstract class BaseActivity extends AppCompatActivity {

    private Unbinder mUnbinder;
    protected MainDialog mainDialog;
    private ShowDialog showDialog;
    private ViewSkeletonScreen mLoading;
    private OnRetryListener onRetryListener;
    private ShowDialog showDialog1;
    private MyPhotoDialog myPhotoDialog;

    private static final String TAG = BaseActivity.class.getSimpleName();


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(setLayoutId());

        initdata();
    }
    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        super.setContentView(layoutResID);

        mUnbinder = ButterKnife.bind(this);
        mainDialog = new MainDialog(this);
        if (isRegisterEventBus()) {
            if (!EventBus.getDefault().isRegistered(this))
                EventBus.getDefault().register(this);
        }
        if (isSetStatusBar()) {
            //沉浸式状态栏
            StatusBarUtil.setColor(this, getResources().getColor(R.color.base_color), 0);
            StatusBarUtil.setDarkMode(this);
        }

   initdata();
    }

    protected abstract int setLayoutId();
    /*
    * 初始化数据
    * */
    protected abstract void initdata();
    @Override
    public void setContentView(View view) {
        super.setContentView(view);

        //mUnbinder = ButterKnife.bind(this);

    }



    @Override
    protected void onPause() {
        super.onPause();
        ToastUtils.cancel();
    }

    @Override
    protected void onStop() {
        super.onStop();
        ToastUtils.cancel();
    }






    public void hideLoading() {
        if (mainDialog.isShowing()) {
            mainDialog.dismiss();
        }
    }

    public void showLoading() {
        mainDialog.show();
    }


    /**
     * 初始化加载状态View
     *
     * @param resourceView        需要被替换的view
     * @param skeletonLayoutResID 替换的view/骨架viewId
     */
    public void initLoadingView(View resourceView, int skeletonLayoutResID) {
        mLoading = Skeleton.bind(resourceView)
                .load(skeletonLayoutResID == 0 ? R.layout.mult_state_loading : skeletonLayoutResID)
                .buid();
    }

    /**
     * 加载成功
     */
    public void hideLoaingView() {
        if (mLoading != null) {
            mLoading.hide();
        }
    }

    /**
     * 通用消息
     *
     * @param tit     标题
     * @param msg     消息
     * @param onClick 点击事件
     */
    public void setShowDialog(String tit, String msg, View.OnClickListener onClick) {
        showDialog1 = new ShowDialog(this);
        showDialog1.setShowDialog(tit, msg, onClick);
        showDialog1.setCanceledOnTouchOutside(false);
        showDialog1.show();
    }

    /**
     * 加载中
     */
    public void showLoadingView() {
        if (mLoading != null) {
            mLoading.show();
        }
    }

    /**
     * 加载错误/失败
     */
    public void showErrorView() {
        if (mLoading != null) {
            View retry = mLoading.error();
            if (retry != null) {
                retry.setOnClickListener(v -> {
                    if (onRetryListener != null) {
                        onRetryListener.onClick();
                    }
                });
            }
        }
    }

    //屏幕变暗，变亮效果
    private void setBgAlpha(float bgAlpha) {
        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        attributes.alpha = bgAlpha;
        //alpha在0.0f和1.0f之间，1.0f完全不暗，0.0f全暗
        if (bgAlpha == 1) {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        } else {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        }
        getWindow().setAttributes(attributes);
    }

    public void setPhotoDialog(View.OnClickListener photoOnClickListener, View.OnClickListener albumOnClickListener) {
        myPhotoDialog = new MyPhotoDialog(this);
        myPhotoDialog.setPhotoDialog(photoOnClickListener, albumOnClickListener);
        myPhotoDialog.setCanceledOnTouchOutside(false);
        myPhotoDialog.show();
        setBgAlpha(0.5f);
        setBgAlpha(1f);
    }

    /**
     * 加载为空数据
     */
    public void showEmptyView() {
        if (mLoading != null) {
            mLoading.empty();
        }
    }

    /**
     * 加载错误/失败重试的点击事件
     *
     * @param onRetryListener
     */
    public void setOnRetryListener(OnRetryListener onRetryListener) {
        this.onRetryListener = onRetryListener;
    }

    public interface OnRetryListener {
        void onClick();
    }

    /**
     * 是否需要设置沉浸状态栏，默认设置
     *
     * @return true表示设置，默认设置，子类需要不默认的话复写此方法返回false.
     */
    protected boolean isSetStatusBar() {
        return true;
    }



    /**
     * 是否注册事件分发
     *
     * @return true绑定EventBus事件分发，默认不绑定，子类需要绑定的话复写此方法返回true.
     */
    protected boolean isRegisterEventBus() {
        return false;
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        super.setContentView(view, params);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
//        unregisterReceiver(mNetWorkChangReceiver);
        ToastUtils.cancel();
        mUnbinder.unbind();
        if (isRegisterEventBus()) {
            if (EventBus.getDefault().isRegistered(this))
                EventBus.getDefault().unregister(this);
        }
    }
}
